﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace HFController
{
    /// <summary>
    /// 工作，招聘业务逻辑
    /// </summary>
    public class JobsController:ControllerBase
    {
        //单利对象
        private static JobsController _jobsControleer;
        //实例化单列对象
        public static JobsController getController()
        {
            if (_jobsControleer == null)
            {
                _jobsControleer = new JobsController();
            }
            return _jobsControleer;
        }
        #region 工作分类

        /// <summary>
        /// 工作分类数据库访问
        /// </summary>
        private hf.DAL.Jobs JobsDAL = new hf.DAL.Jobs();

        /// <summary>
        /// 添加工作分类
        /// </summary>
        /// <param name="JobsName"></param>
        public void doAddJobs(string JobsName)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.Jobs model = new hf.Model.Jobs();

            model.JobsName = JobsName;
            try
            {
                model.ID = JobsDAL.Add(model);

                result.isTrue = true;
                result.Message = "添加成功！";
                result.goUrl = "location='Jobs.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 根据一个ID 得到一条工作分类
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public hf.Model.Jobs getJobsModel(int ID)
        {
            hf.Model.Jobs model = new hf.Model.Jobs();
            int total;

            List<hf.Model.Jobs> list = JobsDAL.GetJobsList(1, 1, "ID=" + ID.ToString(), out total);
            if (list.Count > 0)
            {
                model = list[0];
            }
            else
            {
                model = null;
            }
            return model;
        }

        /// <summary>
        /// 修改工作分类
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="JobsNmae"></param>
        public void doModifyJobs(int ID, string JobsNmae)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.Jobs model = this.getJobsModel(ID);

            model.JobsName = JobsNmae;

            try
            {
                //执行修改
                JobsDAL.Update(model);

                result.isTrue = true;
                result.Message = "修改成功！";
                result.goUrl = "location='Jobs.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        public List<hf.Model.Jobs> getJobsList(int Count)
        {
            List<hf.Model.Jobs> list = new List<hf.Model.Jobs>();

            //没有显示记录
            int PageSize = 10;
            //当前页码
            int PageNumber = 1;
            //记录总条数
            int total;
            //筛选条件
            string strSql = "";

            //获得列表
            list = JobsDAL.GetJobsList(PageSize, PageNumber, strSql, out total);

            return list;
        }

        /// <summary>
        /// 得到列表 前台用
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageNumber"></param>
        /// <param name="strWhere"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public List<hf.Model.Jobs> getJobsList(int PageSize ,int PageNumber ,string strWhere ,out int total)
        {
            List<hf.Model.Jobs> list = new List<hf.Model.Jobs>();
            if (HttpContext.Current.Request.QueryString["Page"] != null)
            {
                PageNumber = int.Parse(HttpContext.Current.Request.QueryString["Page"].ToString());
            }

            //获得列表
            list = JobsDAL.GetJobsList(PageSize, PageNumber, strWhere, out total);

            return list;
        }

        /// <summary>
        /// 得到工作分类列表
        /// </summary>
        /// <param name="PagerStr"></param>
        /// <returns></returns>
        public List<hf.Model.Jobs> getJobsList(out string PagerStr)
        {
            List<hf.Model.Jobs> list = new List<hf.Model.Jobs>();

            //没有显示记录
            int PageSize = 10;
            //当前页码
            int PageNumber = 1;
            if (HttpContext.Current.Request.QueryString["Page"] != null)
            {
                PageNumber = int.Parse(HttpContext.Current.Request.QueryString["Page"].ToString());
            }
            //记录总条数
            int total;
            //筛选条件
            string strSql = "";

            //获得列表
            list = JobsDAL.GetJobsList(PageSize, PageNumber, strSql, out total);

            //分页
            PagerStr = LLL.Pager.TrunPageHtmlCode(PageSize, PageNumber, total, "");

            return list;
        }

        /// <summary>
        /// 发布应聘信息
        /// </summary>
        /// <param name="names"></param>
        /// <param name="sex"></param>
        /// <param name="marriage"></param>
        /// <param name="height"></param>
        /// <param name="school"></param>
        /// <param name="specialty"></param>
        /// <param name="academic"></param>
        /// <param name="language"></param>
        /// <param name="computer"></param>
        /// <param name="isexamination"></param>
        /// <param name="phone"></param>
        /// <param name="personweb"></param>
        /// <param name="accountplace"></param>
        /// <param name="nowplace"></param>
        /// <param name="addr"></param>
        /// <param name="jobsid"></param>
        /// <param name="provinceid"></param>
        /// <param name="cityid"></param>
        /// <param name="treatment"></param>
        /// <param name="personinfo"></param>
        /// <param name="workexe"></param>
        /// <returns></returns>
        public HFModel.ResultModel doAddWanted(string names,
            string sex,
            string marriage,
            string height,
            string school,
            string specialty,
            string academic,
            string language,
            string computer ,
            string isexamination,
            string phone,
            string personweb,
            string accountplace ,
            string nowplace ,
            string addr,
            string jobsid,
            string provinceid ,
            string cityid,
            string treatment ,
            string personinfo ,
            string workexe)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            hf.Model.Wanted model = new hf.Model.Wanted();
            BagePages bp = new BagePages();
            hf.Model.User user = bp.getLoginAccount();
            if (user == null)
            {
                result.isTrue = false;
                result.Message = "请先登录";
            }
            else {
                model.Names = names;
                model.JobsID = int.Parse(jobsid);
                model.JobsName = JobsController.getController().getJobsModel(model.JobsID).JobsName;
                model.Sex = int.Parse(sex);
                model.Marriage = int.Parse(marriage);
                model.Height = int.Parse(height);
                model.School = school;
                model.Specialty = specialty;
                model.Academic = academic;
                model.Language = language;
                model.Computer = computer;
                model.isExamination = int.Parse(isexamination);
                model.Phone = phone;
                model.PersonWeb = personweb;
                model.AccountPlace = accountplace;
                model.NowPlace = nowplace;
                model.Addr = addr;
                model.WorkProvinceID = int.Parse(provinceid);
                model.WorkCityID = int.Parse(cityid);
                model.Treatment = treatment;
                model.PersonInfo = personinfo;
                model.Workexe = workexe;
                model.Addtime = DateTime.Now;
                model.Birthday = DateTime.Now;
                try
                {
                    result.isTrue = true;
                    result.Message = "发布成功！感谢您发布信息！";
                    WantedDAL.Add(model);
                }
                catch (Exception err){
                    result.isTrue = false;
                    result.Message = "系统异常，请联系管理员\n" + err.Message;
                }

            }
            return result;
        }

        /// <summary>
        /// 删除工作分类
        /// </summary>
        /// <param name="ID"></param>
        public void doDelegeJobs(int ID)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            try
            {
                JobsDAL.Delete(ID);

                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='Jobs.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        } 
        #endregion

        #region 求职信息

        /// <summary>
        /// 求职信息数据库访问
        /// </summary>
        private hf.DAL.Wanted WantedDAL = new hf.DAL.Wanted();

        /// <summary>
        /// 删除求职信息
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public HFModel.ResultModel deleteWanted(int ID)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            BagePages bp = new BagePages();
            hf.Model.User user = bp.getLoginAccount();
            if (user == null)
            {
                result.isTrue = false;
                result.Message = "请先登录！";
            }
            else {
                try
                {
                    WantedDAL.Delete(ID);
                    result.isTrue = true;
                    result.Message = "删除成功";
                }
                catch (Exception Err)
                {
                    result.isTrue = false;
                    result.Message = Err.Message;
                }
            }

            return result;
        }

        /// <summary>
        /// 根据ID 得到一条求职信息列
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public hf.Model.Wanted getWantedModel(int ID)
        {
            hf.Model.Wanted model = new hf.Model.Wanted();
            int total;

            List<hf.Model.Wanted> list = WantedDAL.GetWantedList(1, 1, "ID=" + ID.ToString(), out total);

            if (list.Count > 0)
            {
                model = list[0];
            }
            else
            {
                model = null;
            }
            return model;
        }

        /// <summary>
        /// 根据数量得到求职信息列表
        /// </summary>
        /// <param name="Count"></param>
        /// <returns></returns>
        public List<hf.Model.Wanted> getWantedList(int Count)
        {
            List<hf.Model.Wanted> list = new List<hf.Model.Wanted>();

            //没有显示记录
            int PageSize = Count;

            //记录总条数
            int total;
            //筛选条件
            string strSql = "";

            //获得列表
            list = WantedDAL.GetWantedList(PageSize, 1, strSql, out total);

            foreach (hf.Model.Wanted w in list)
            {
                w.JobsName = this.BindJobsName(w.JobsID);
            }

            return list;
        }

        /// <summary>
        /// 得到列表，前台用
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageNumber"></param>
        /// <param name="strWhere"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public List<hf.Model.Wanted> getWantedList(int PageSize, int PageNumber, string strWhere, out int total)
        {
            List<hf.Model.Wanted> list = new List<hf.Model.Wanted>();

            //获得列表
            list = WantedDAL.GetWantedList(PageSize, PageNumber, strWhere, out total);

            foreach (hf.Model.Wanted w in list)
            {
                w.JobsName = this.BindJobsName(w.JobsID);
            }

            return list;
        }

        /// <summary>
        /// 得到求职信息列表
        /// </summary>
        /// <param name="PagerStr"></param>
        /// <returns></returns>
        public List<hf.Model.Wanted> getWantedList(out string PagerStr)
        {
            List<hf.Model.Wanted> list = new List<hf.Model.Wanted>();

            //没有显示记录
            int PageSize = 10;
            //当前页码
            int PageNumber = 1;
            if (HttpContext.Current.Request.QueryString["Page"] != null)
            {
                PageNumber = int.Parse(HttpContext.Current.Request.QueryString["Page"].ToString());
            }
            //记录总条数
            int total;
            //筛选条件
            string strSql = "";

            //获得列表
            list = WantedDAL.GetWantedList(PageSize, PageNumber, strSql, out total);

            foreach (hf.Model.Wanted w in list)
            {
                w.JobsName = this.BindJobsName(w.JobsID);
            }

            //分页
            PagerStr = LLL.Pager.TrunPageHtmlCode(PageSize, PageNumber, total, "");

            return list;
        }

        /// <summary>
        /// 删除求职信息
        /// </summary>
        /// <param name="ID"></param>
        public void doDelegeWanted(int ID)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();

            try
            {
                WantedDAL.Delete(ID);

                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='WantedList.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="IDS"></param>
        public void doDelegeWanted(string[] IDS)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            try
            {
                for (int i = 0; i < IDS.Length; i++)
                {
                    WantedDAL.Delete(int.Parse(IDS[i].Trim()));
                }
                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='WantedList.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }
        #endregion

        #region 招聘信息

        /// <summary>
        /// 招聘信息数据访问
        /// </summary>
        private hf.DAL.Recruitment RecruitmentDAL = new hf.DAL.Recruitment();

        /// <summary>
        /// 得到指定数量的招聘信息列表
        /// </summary>
        /// <param name="Count"></param>
        /// <returns></returns>
        public List<hf.Model.Recruitment> getRecruitmentList(int Count)
        {
            List<hf.Model.Recruitment> list = new List<hf.Model.Recruitment>();

            //没有显示记录
            int PageSize = Count;

            //记录总条数
            int total;
            //筛选条件
            string strSql = "";

            //获得列表
            list = RecruitmentDAL.GetRecruitmentList(PageSize, 1, strSql, out total);
            foreach (hf.Model.Recruitment w in list)
            {
                w.JobsName = this.BindJobsName(w.JobsID);
            }
            return list;
        } 

        /// <summary>
        /// 得到招聘信息列表
        /// </summary>
        /// <param name="PagerStr"></param>
        /// <returns></returns>
        public List<hf.Model.Recruitment> getRecruitmentList(out string PagerStr)
        {
            List<hf.Model.Recruitment> list = new List<hf.Model.Recruitment>();

            //没有显示记录
            int PageSize = 10;
            //当前页码
            int PageNumber = 1;
            if (HttpContext.Current.Request.QueryString["Page"] != null)
            {
                PageNumber = int.Parse(HttpContext.Current.Request.QueryString["Page"].ToString());
            }
            //记录总条数
            int total;
            //筛选条件
            string strSql = "";

            //获得列表
            list = RecruitmentDAL.GetRecruitmentList(PageSize, PageNumber, strSql, out total);
            foreach (hf.Model.Recruitment w in list)
            {
                w.JobsName = this.BindJobsName(w.JobsID);
            }
            //分页
            PagerStr = LLL.Pager.TrunPageHtmlCode(PageSize, PageNumber, total, "");

            return list;
        }

        /// <summary>
        /// 得到招聘信息列表，前台用
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageNumber"></param>
        /// <param name="strWhere"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public List<hf.Model.Recruitment> getRecruitmentList(int PageSize ,int PageNumber, string strWhere ,out int total)
        {
            List<hf.Model.Recruitment> list = new List<hf.Model.Recruitment>();


            //获得列表
            list = RecruitmentDAL.GetRecruitmentList(PageSize, PageNumber, strWhere, out total);
            foreach (hf.Model.Recruitment w in list)
            {
                w.JobsName = this.BindJobsName(w.JobsID);
            }

            return list;
        }

        /// <summary>
        /// 保存招聘信息
        /// </summary>
        /// <param name="endtimeCount"></param>
        /// <param name="email"></param>
        /// <param name="cityid"></param>
        /// <param name="provinceid"></param>
        /// <param name="phone"></param>
        /// <param name="delegatepersontype"></param>
        /// <param name="delegatename"></param>
        /// <param name="delegatemoney"></param>
        /// <param name="involvedcasemoney"></param>
        /// <param name="delegateclaim"></param>
        /// <param name="delegatecontent"></param>
        /// <param name="classid"></param>
        /// <returns></returns>
        public HFModel.ResultModel doAddRecruitment(
                string jobsid ,
                string personcount ,
                string contact,
                string academic,
                string endtimeCount ,
                string phone ,
                string email ,
                string addr ,
                string content ,
                string unitcontent  ,
            string unit
            )
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            BagePages bp = new BagePages();
            hf.Model.Lawyer lawyer = bp.getLoginLawyer();
            if (lawyer == null)
            {
                result.isTrue = false;
                result.Message = "请先登录";
            }
            else {
                hf.Model.Recruitment model = new hf.Model.Recruitment();
                model.Effectivetime = DateTime.Now.AddDays(int.Parse(endtimeCount));
                model.Email = email;
                model.JobsID = int.Parse(jobsid);
                model.PersonCount = int.Parse(personcount);
                model.Contact = contact;
                model.Academic = academic;
                model.Phone = phone;
                model.Addr = addr;
                model.Content = content;
                model.Unit = unit;
                model.LaywerID = lawyer.ID;
                model.JobsName = JobsController.getController().getJobsModel(model.JobsID).JobsName;
                model.Addtime = DateTime.Now;
                try {
                    RecruitmentDAL.Add(model);
                    result.isTrue = true;
                    result.Message = "信息发布成功";
                }
                catch (Exception err)
                {
                    result.isTrue = false;
                    result.Message = err.Message;
                }
            }
            return result;
        }

        /// <summary>
        /// 删除招聘
        /// </summary>
        /// <param name="ID"></param>
        public void doDelegeRecruitment(int ID)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();

            try
            {
                RecruitmentDAL.Delete(ID);

                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='RecruitmentList.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="IDS"></param>
        public void doDelegeRecruitment(string[] IDS)
        {
            HFModel.ResultModel result = new HFModel.ResultModel();
            try
            {
                for (int i = 0; i < IDS.Length; i++)
                {
                    RecruitmentDAL.Delete(int.Parse(IDS[i].Trim()));
                }
                result.isTrue = true;
                result.Message = "删除成功！";
                result.goUrl = "location='RecruitmentList.aspx';";
                result.historyUrl = "history.go(-1)";
            }
            catch
            {
                result.isTrue = false;
                result.Message = "系统问题，清联系管理员";
                result.goUrl = "history.go(-1)";
                result.historyUrl = "history.go(-1)";
            }
            this.doResult(result);
        }

        #endregion

        /// <summary>
        /// 私有方法，绑定得到工作岗位名称
        /// </summary>
        /// <param name="JobID"></param>
        /// <returns></returns>
        private string BindJobsName(int JobID)
        {
            //return this.getJobsModel(JobID).JobsName;
            if (this.getJobsModel(JobID) != null)
            {
                return this.getJobsModel(JobID).JobsName;
            }
            else
            {
                return "";
            }
        }
    }
}
